#include <math.h>

#define pitchPin     0
#define speakerOut  11

int pitchval = 1;
int val = 0;
int wait = 100;
byte varRead=0;
byte buff=0;

int freq,t;

float noteval;
//note values
float C     = 130.8;
float CS    = 138.6;
float D     = 146.8;
float DS    = 155.6;
float E     = 164.8;
float F     = 174.6;
float FS    = 185;
float G     = 196;
float GS    = 207.7;
float A2    = 220;
float A2S   = 233.1;
float B2    = 246.9;
float C2    = 261.6;
float C2S   = 277.2;
float D2    = 293.7;
float D2S   = 311.1;
float E2    = 329.6;
float F2    = 349.2;
float F2S   = 370;
float G2    = 392;
float G2S   = 415.3;
float A3    = 440;


void setup() {
  pinMode(speakerOut, OUTPUT);
  pinMode(12, INPUT);
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(12, HIGH);
  digitalWrite(13,LOW);

  if(digitalRead(12)==LOW) varRead=0;
  else varRead=1;

  if(varRead!=buff){

    varRead=buff;
    digitalWrite(13,HIGH);

    if( analogRead(pitchPin) > 150)
    {
      pitchval = (analogRead(pitchPin) / 100);  //a shot in the dark
    }
    else if(analogRead(pitchPin) <= 15)
    {
      pitchval = 1;
    }
    Serial.println(pitchval);
    //Happy Bithday you
    noteval = C2;
    freqout((int)noteval,300);  
    noteval = C2;
    freqout((int)noteval,300);

    noteval = D2;
    freqout((int)noteval,500);

    noteval = C2;
    freqout((int)noteval,500);
    delay(wait);

    noteval = F2;
    freqout((int)noteval,500);
    delay(wait);

    noteval = E2;
    freqout((int)noteval,1200);
    delay(wait*2);

    //Happy Bithday to you
    noteval = C2;
    freqout((int)noteval,300);  
    noteval = C2;
    freqout((int)noteval,300);

    noteval = D2;
    freqout((int)noteval,500);

    noteval = C2;
    freqout((int)noteval,500);
    delay(wait);

    noteval = G2;
    freqout((int)noteval,500);
    delay(wait);

    noteval = F2;
    freqout((int)noteval,1200);
    delay(wait*2);


    noteval = C2;
    freqout((int)noteval,300);
    noteval = C2;
    freqout((int)noteval,300);

    noteval = C2*2;
    freqout((int)noteval,500);

    noteval = A2*2;
    freqout((int)noteval,500);

    noteval = F2;
    freqout((int)noteval,300);
    noteval = F2;
    freqout((int)noteval,300);

    noteval = E2;
    freqout((int)noteval,500);

    noteval = D2;
    freqout((int)noteval,500);
    delay(wait);

    noteval = B2*2;
    freqout((int)noteval,300);  
    noteval = B2*2;
    freqout((int)noteval,300);
    delay(wait);

    noteval = A2*2;
    freqout((int)noteval,500);
    delay(wait);
    noteval = F2;
    freqout((int)noteval,500);
    delay(wait);
    noteval = G2;
    freqout((int)noteval,500);
    delay(wait);
    noteval = F2;
    freqout((int)noteval,1500);
  }
}

//freqout code by Paul Badger (and hacked a bit by me)
void freqout(int freq, int t) 
{ 
  int hperiod;     //calculate 1/2 period in us 
  long cycles, i; 
  hperiod = (500000 / ((freq - 7) * pitchval));             // subtract 7 us to make up for digitalWrite overhead - determined empirically 
  // calculate cycles 
  cycles = ((long)freq * (long)t) / 1000;    // calculate cycles 

  for (i=0; i<= cycles; i++){              // play note for t ms  
    digitalWrite(speakerOut, HIGH);  
    delayMicroseconds(hperiod); 
    digitalWrite(speakerOut, LOW);  
    delayMicroseconds(hperiod - 1);     // - 1 to make up for fractional microsecond in digitaWrite overhead 
  } 
}
